package com.cold.demo.template.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.websocket.Extension;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.util.List;


@Import(value = {WebSocketServer.class})
public class WebSocketConfig extends ServerEndpointConfig.Configurator{

    private static final Logger log = LoggerFactory.getLogger(WebSocketConfig.class);

    /**
     * 修改握手信息
     */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 可根据自己实际的业务逻辑需求在此进行参数的调整，只有第一次进行调用onOpen的时候才会执行该方法
        super.modifyHandshake(sec, request, response);
    }

    @Override
    public String getNegotiatedSubprotocol(List<String> supported, List<String> requested) {
        return super.getNegotiatedSubprotocol(supported, requested);
    }

    @Override
    public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) {
        return super.getNegotiatedExtensions(installed, requested);
    }

    @Override
    public boolean checkOrigin(String originHeaderValue) {
        return super.checkOrigin(originHeaderValue);
    }

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        return super.getEndpointInstance(clazz);
    }

    // 上面几个方法的执行顺序为：checkOrigin --> getNegotiatedSubprotocol --> getNegotiatedExtensions --> modifyHandshake --> getEndpointInstance




    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        //这个对象说一下，貌似只有服务器是 tomcat 的时候才需要配置,具体我没有研究
        return new ServerEndpointExporter();
    }
}
